package main

import (
	"fmt"

	"gitee.com/minph/fileScript/common"
	"gitee.com/minph/fileScript/core"
	"github.com/minph/fs"
	"github.com/minph/fs/utils"
)

// RunFile --run file 对应命令
func RunFile(c *Config) {

	if !utils.Exist(c.File) {
		errorType := "Error@FileScript.RunFile.Unwrap[FILE_NOT_FOUND]"
		common.PrintErrorAndExit(errorType, fmt.Sprintf("文件路径[%v]不正确", c.File))

	}

	content, err := fs.ReadLines(c.File)

	if err != nil {
		errorType := "Error@FileScript.RunFile.Unwrap[FILE_CANNOT_READ]"
		common.PrintErrorAndExit(errorType, fmt.Sprintf("读取文件[%v]内容失败", c.File))

	}
	code := core.CompressCode(content)
	standardCode := core.Standardize(code.CheckCode()) // 标准化后代码，且开启作用域注解
	trees := core.GenerateTrees(standardCode)          // 解析语法树

	for _, tree := range trees {
		if !tree.Skip {
			tree.Node.Run()
		}
	}

}
